﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AppLab;
using AppLab.Models;
using AppLab.Web;
using AppLab.Web.Services;

namespace AppLab.Views
{
    public partial class SearchControl : UserControl
    {
        private FlowContext flow = new FlowContext();

        public SearchControl()
        {
            InitializeComponent();
            BindComBoxOfBrand();
            BindAutoCompleteOfMobileType();
        }

        public Brand Brand{ set; get; }

        public string MobileTypeStr { set; get; }

        #region 品牌

        private void BindComBoxOfBrand()
        {
            var query = flow.GetBrandsQuery();
            var loadBrand = flow.Load(query);
            loadBrand.Completed += new EventHandler(loadBrand_Completed);
        }

        void loadBrand_Completed(object sender, EventArgs e)
        {
            LoadOperation loadOp = (LoadOperation)sender;
            mobileBrand.ItemsSource = loadOp.Entities;
            mobileBrand.DisplayMemberPath = "BrandName";
        }

        #endregion

        #region 型号
        //保存所有的手机型号
        private ComboBox comb = new ComboBox();
        private void BindAutoCompleteOfMobileType()
        {
            var query = flow.GetMobileTypesQuery(-1);

            LoadOperation<MobileType> load = flow.Load(query);
            load.Completed += new EventHandler(load_Completed);
        }

        void load_Completed(object sender, EventArgs e)
        {
            LoadOperation loadOp = (LoadOperation)sender;
            comb.ItemsSource = loadOp.Entities;
            BindAutoCompleteBox(comb);
        }

        private void BindAutoCompleteBox(ComboBox comb)
        {
            List<String> listStr = new List<string>();
            for (int i = 0; i < comb.Items.Count; i++)
            {
                MobileType e = comb.Items[i] as MobileType;
                listStr.Add(e.TypeName);
            }
            autoCompleteBoxMbBrand.ItemsSource = listStr;
        }

        #endregion

        private void mobileBrand_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (mobileBrand.SelectedItem != null)
            {
                this.autoCompleteBoxMbBrand.Text = string.Empty;
                this.Brand = this.mobileBrand.SelectedItem as Brand;
            }
            else
            {
                this.Brand = null;
            }
        }

        private void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            this.mobileBrand.SelectedItem = null;
            if (string.IsNullOrEmpty(autoCompleteBoxMbBrand.Text.Replace(" ", "")))
            {
                this.MobileTypeStr = string.Empty;
            }
            else
            {
                this.MobileTypeStr = this.autoCompleteBoxMbBrand.Text.Replace(" ", "");
            }
        }

        private void autoCompleteBoxMbBrand_GotFocus(object sender, RoutedEventArgs e)
        {
            this.mobileBrand.SelectedItem = null;
        }
    }
}
